package com.wanxi.dao.impl;

import com.wanxi.dao.CuisineDao;
import com.wanxi.model.CuisineModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CuisineDaoImpl implements CuisineDao {
    @Override
    public int add(CuisineModel cuisineModel) {
        String sql = "insert into cuisine (name,create_time,enable) values('" + cuisineModel.getName() + "',now(),'0')";
        return JDBC.update(sql);
    }

    @Override
    public int update(CuisineModel cuisineModel) {
        String sql = "update cuisine set name='" + cuisineModel.getName() + "',enable='" + cuisineModel.getEnable() + "' where id= " + cuisineModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public int delete(CuisineModel cuisineModel) {
//        form
        String sql = "delete from cuisine where id=" + cuisineModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public List<CuisineModel> findAll(CuisineModel cuisineModel) {
//        有可能没有传数据，则属性值为null，需要判断为null的情况不能拼接到sql语句
        String sql = "select * from cuisine where 1=1 ";
        if (cuisineModel.getName() != null) {
            sql += " and name like '%" + cuisineModel.getName() + "%'";
        }
        if (cuisineModel.getEnable() != null) {
            sql += " and enable='" + cuisineModel.getEnable() + "'";
        }
        ResultSet query = JDBC.query(sql);
        List<CuisineModel> list = new ArrayList<>();
        try {
            while (query.next()) {
                CuisineModel model = CuisineModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getString("create_time"))
                        .build();
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public int count(CuisineModel cuisineModel) {
        String sql = "select count(*) count from cuisine where 1=1";
        if (cuisineModel.getName() != null) {
            sql += " and name like '%" + cuisineModel.getName() + "%'";
        }
        if (cuisineModel.getEnable() != null) {
            sql += " and enable='" + cuisineModel.getEnable() + "'";
        }
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return query.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public CuisineModel findById(CuisineModel cuisineModel) {
        String sql = "select * from cuisine where id=" + cuisineModel.getId();
        CuisineModel model = new CuisineModel();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return CuisineModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getString("create_time"))
                        .build();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }
}
